package com.example.rule.core;

import com.example.rule.db.entity.PymtRule;
import com.example.rule.enums.SelectorEnum;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Jason - Chusifeng
 * @version 1.0.0
 * @ClassName Condition.java
 * @PackageName com\welab\vb\payment\transaction\rule\core\Condition.java
 * @Description 条件父类，指定当事实输入时，使用condition判断是否满足条件
 * @createTime 2022年10月10日 10:48:42
 */
public interface Condition {

    SpelParserConfiguration CONFIG = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
            Condition.class.getClassLoader());
    ExpressionParser PARSER = new SpelExpressionParser(CONFIG);
    boolean evaluate(Facts facts);


    void addExpression(String expressionStr);
    void setSelector(SelectorEnum selectorEnum);

    default String[] createExpressStr(Representation representation, PymtRule rule){
        return null;
    }

    /**
     * 判断时间
     * @param time fact 入参时间
     * @param value 规则设定的时间
     * @return
     */
    static boolean isBefore(String time,String value){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time,formatter).isBefore(LocalDateTime.parse(value,formatter));
    }
    static boolean isAfter(String time,String value){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time,formatter).isAfter(LocalDateTime.parse(value,formatter));
    }
    static boolean isEqual(String time,String value){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time,formatter).isEqual(LocalDateTime.parse(value,formatter));
    }
}
